import { get as _get } from 'object-path';
import { getRuleList, addRule, getRuleDetail } from '@/services/setting/ruleManagement';

export default {
    namespace: 'ruleManagement',
    state: {
        ruleList: [],
        detailData: {}
    },
    effects: {
        *fetchRuleList({ payload }, { put, call }) {
            const result = yield call(getRuleList, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        ruleList: _get(result, 'data', [])
                    }
                });
            }
        },
        *fetchAddRule({ payload, callback }, { call }) {
            const result = yield call(addRule, payload);
            if (callback) callback(result);
        },
        *fetchRuleDetail({ payload, callback }, { put, call }) {
            const result = yield call(getRuleDetail, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        detailData: _get(result, 'data', {})
                    }
                });
            }
            if (callback) callback(result);
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        clear(state) {
            return {
                ...state,
                detailData: {
                    exceptionSimpleDTOS: [
                        {
                            code: '',
                            endMinute: null,
                            name: '',
                            startMinute: null,
                            type: null
                        },
                        {
                            code: '',
                            endMinute: null,
                            name: '',
                            startMinute: null,
                            type: null
                        },
                        {
                            code: '',
                            endMinute: null,
                            name: '',
                            startMinute: 0,
                            type: null
                        },
                        {
                            code: '',
                            endMinute: null,
                            name: '',
                            startMinute: null,
                            type: null
                        },
                        {
                            code: '',
                            endMinute: 9999,
                            name: '',
                            startMinute: null,
                            type: null
                        }
                    ],
                    ruleVO: {
                        flexibleStartTime: null,
                        flexibleWork: 0,
                        institutionId: null,
                        institutionName: '',
                        leaveTime: '',
                        workHours: null,
                        workTime: ''
                    },
                    groupName: '员工默认采用的考勤规则'
                }
            };
        }
    }
};
